package com.fog.judge.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fog.judge.common.Base;
import com.fog.judge.common.UserContext;
import com.fog.judge.entity.Teacher;
import com.fog.judge.entity.User;
import com.fog.judge.service.QualitativeEvaluationsService;
import com.fog.judge.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private QualitativeEvaluationsService qualitativeEvaluationsService;

    @PostMapping("/login")
    public Base login(@RequestBody User user){

        User user1 = userService.getOne(new QueryWrapper<User>().eq("username",user.getUsername()));

        if(user1!=null){
            if (user1.getPassword().equals(user.getPassword())){
                user.setId(user1.getId());
                UserContext.setUserId(user.getId());
                if(user1.getRole()==0){
                    return Base.success("admin");
                }
                if(user1.getRole()==1){
                    return Base.success("student");
                }
                if(user1.getRole()==2){
                    return Base.success("teacher");
                }
            }
        }
        return Base.error("失败");
    }

    @GetMapping("/page")
    public Base page(int page, int pageSize,String name){
        // 创建分页对象，参数一是当前页数，参数二是每页显示多少条
        Page<User> pageInfo = new Page<>(page, pageSize);
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.orderByDesc("full_name");
        if(name !=null){
            userQueryWrapper.like("full_name", name);
        }
        // 调用userService中的方法进行分页查询
        userService.page(pageInfo,userQueryWrapper);
        return Base.success(pageInfo);
    }

    @PostMapping
    public Base add(@RequestBody User user){
        boolean exists = userService.existsByUsername(user.getUsername());
        if(exists){
            return Base.error("用户已存在");
        }
        boolean result = userService.save(user);
        if (result) {
            return Base.success("success");
        }else {
            return Base.error("error");
        }
    }
    @GetMapping("/{id}")
    public Base getUserById(@PathVariable Long id){
        User user = userService.getById(id);
        return Base.success(user);
    }
    @DeleteMapping
    public Base deleteUser(String ids) {
        // 根据用户名删除用户

        userService.deleteUser(ids);

        return Base.success("用户删除成功");
    }
    @DeleteMapping("/student")
    public Base deleteStudent(String ids) {
        // 根据用户名删除用户

        userService.deleteUser(ids);

        return Base.success("用户删除成功");
    }
    @PostMapping("/update")
    public Base update(@RequestBody User user){
        boolean result = userService.updateById(user);
        if(result){
            return Base.success("用户信息修改成功");
        }else{
            return Base.error("用户信息修改失败");
        }
    }

    @PostMapping("/logout")
    public Base logout(HttpServletRequest request){
        request.getSession().removeAttribute("student");
        request.getSession().removeAttribute("teacher");
        return Base.success(null);
    }

    @GetMapping("/student/page")
    public Base pageStudent(int page , int pageSize , String name){
        //log.info(UserContext.getUserId().toString());
        // 创建分页
        Page<User> pageInfo = new Page<>(page, pageSize);
        QueryWrapper<User> studentQueryWrapper = new QueryWrapper<>();
        studentQueryWrapper.orderByDesc("full_name");
        if(name !=null){
            studentQueryWrapper.like("full_name", name);
        }
        studentQueryWrapper.eq("role",1);
        userService.page(pageInfo, studentQueryWrapper);
        return Base.success(pageInfo);
    }

    @PostMapping("/student")
    public Base addStudent(@RequestBody User user){
        boolean exists = userService.existsByUsername(user.getUsername());
        if(exists){
            return Base.error("用户已存在");
        }
        boolean result = userService.save(user);
        if (result) {
            return Base.success("success");
        }else {
            return Base.error("error");
        }
    }
}
